﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace personalplaner.common.controls
{
	/// <remarks>
	/// var colorToBrush = new ColorToSolidColorBrushConverter();
	/// 
	/// var col = new DataGridPlanItemColumn
	///	{
	///		IsReadOnly = true,
	///		CanUserReorder = false,
	///
	///		Header = column.Datum.Day,
	///
	///		TextBinding = new Binding(string.Format("[{0}].Name", index)),
	///		BackgroundBinding = new Binding(string.Format("[{0}].Color", index))
	///		{
	///			Converter = colorToBrush
	///		},
	///	};
	/// </remarks>
	
	/// <summary>
	/// 
	/// </summary>
	public class DataGridPlanItemColumn : DataGridColumn
	{
		#region Fields

		Border _border;
		TextBlock _textblock;

		#endregion

		#region Properties

		BindingBase _textbinding;
		public virtual BindingBase TextBinding
		{
			get
			{
				return _textbinding;
			}
			set
			{
				if (_textbinding != value)
				{
					_textbinding = value;
					CoerceValue(IsReadOnlyProperty);
					CoerceValue(SortMemberPathProperty);

					NotifyPropertyChanged("TextBinding"); 
				}
			}
		}

		BindingBase _backgroundbinding;
		public virtual BindingBase BackgroundBinding
		{
			get
			{
				return _backgroundbinding;
			}
			set
			{
				if (_backgroundbinding != value)
				{
					_backgroundbinding = value;
					CoerceValue(IsReadOnlyProperty);
					CoerceValue(SortMemberPathProperty);

					NotifyPropertyChanged("BackgroundBinding");
				}
			}
		}
		
        public HorizontalAlignment HorizontalAlignment
        {
            get { return (HorizontalAlignment)GetValue(HorizontalAlignmentProperty); }
            set { SetValue(HorizontalAlignmentProperty, value); }
        }

        public static readonly DependencyProperty HorizontalAlignmentProperty =
            DependencyProperty.Register("HorizontalAlignment", typeof(HorizontalAlignment), typeof(DataGridPlanItemColumn),
                                          new FrameworkPropertyMetadata(HorizontalAlignment.Center, NotifyPropertyChangeForRefreshContent));

        /// <summary>
        /// Method used as property changed callback for properties which need RefreshCellContent to be called
        /// </summary>
        /// <param name="d"></param> 
        /// <param name="e"></param>
        internal static void NotifyPropertyChangeForRefreshContent(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Debug.Assert(d is DataGridPlanItemColumn, "d should be a DataGridPlanItemColumn");

            ((DataGridPlanItemColumn)d).RaisePropertyChanged(e.Property.Name);
        }

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            NotifyPropertyChanged(propertyName);
        }

		#endregion

		#region Implementation

		protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
		{
			throw new NotImplementedException();
		}

		protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
		{
			_border = new Border();
            ApplyBackgroundBinding(_border, Border.BackgroundProperty);

            _textblock = new TextBlock
            {
                HorizontalAlignment = HorizontalAlignment,
                Margin = new Thickness(2,1,2,1)
            };
            ApplyTextBinding(_textblock, TextBlock.TextProperty);

			_border.Child = _textblock;

			return _border;
		}

		internal void ApplyTextBinding(DependencyObject target, DependencyProperty property)
		{
			BindingBase binding = TextBinding;
			if (binding != null)
			{
				BindingOperations.SetBinding(target, property, binding);
			}
			else
			{
				BindingOperations.ClearBinding(target, property);
			}
		}

		internal void ApplyBackgroundBinding(DependencyObject target, DependencyProperty property)
		{
			BindingBase binding = BackgroundBinding;
			if (binding != null)
			{
				BindingOperations.SetBinding(target, property, binding);
			}
			else
			{
				BindingOperations.ClearBinding(target, property);
			}
		}

		#endregion
	}
}
